ID_PartsChildren <- function(PartTree) {
  
  # This function indexes the children for each component.
  #
  # Inputs
  #   PartTree - 1xB vector - gives the part's level in the hierarch, higher = down rabbit hole
  #     Since this is for a specific rabbit hole, the highest level can have more than one entry.
  #
  # Outputs
  #   BranchIdx - 1xN list vector - Lists the index of each component's sub-components (children)
  # 
  # Written by: Sean Gonzalez - WILLCOR, Inc. - October, 2012
  #_____________________________________________________________
  
  pcird <- file("ProbCompIsReplaced.data", "a")
  
  # Initialize variables
  N <- length(PartTree)
#   cat("Length of PartTree =",N,"\n")
  levels <- sort(unique(PartTree))
  
  # Begin with each component, and step through the children 
  # until you come to a component at your own level, and stop.
  BranchIdx  <- list() # ChildIdx
  BranchTree <- list()
  BranchIdx[[1]] = 2:N
  BranchTree[[1]] = PartTree[BranchIdx[[1]]]
  for (c in 2:N) {
    # Gather the number of components in that branch.
#     l = PartTree[c]
#     lx = PartTree == l
#     cat("Evaluating Parent - ",c,"\n")

    found = FALSE
    b = c+1
#     cat("Parent Tree begins with - ",b,"\n")
    while (!found && b<N) {
      if (PartTree[c] >= PartTree[b]) {#lx[b]) {
        if (b==c+1) { 
#           cat("Component ",c," has no children \n")
          BranchIdx[[c]] = 0
          found = TRUE
        } # If there are no sub-components
        else {        
#           cat("Component ",c," has children",(c+1):(b-1),"\n")
          BranchIdx[[c]] = (c+1):(b-1) # Index of branch's components
          BranchTree[[c]] = PartTree[BranchIdx[[c]]]
          found = TRUE
        }
      }
      else { b = b+1 }
#       cat("Parent Tree may include - ",(c+1):(b-1),"\n")
    }
    if (!found) {# Assume it was the last component in the tree at that level
      cat("Component w/ unfound children =",c,"\n",file=pcird)
      BranchIdx[[c]]  = 0 #min(N,c+1):N
      BranchTree[[c]] = PartTree[BranchIdx[[c]]]
      found = TRUE
    }
  }
  
  # Create the associative list
  childAssocList <- integer(2*N)
  cal = 0
  for (c in 1:N) {
    children = BranchIdx[[c]]
    childAssocList[2*cal+1:2] = c(c,children[1]) # USING ONLY THE FIRST CHILD IS WRONG!!
    cal = cal +1
  }
  
#   library(igraph)
#   childAssocGraph <- graph(childAssocList, n=max(childAssocList), directed=FALSE);
#   plot.igraph(childAssocGraph,layout=layout.fruchterman.reingold,vertex.size=12) #,layout=layout.kamada.kawai)
#   print(BranchIdx)
#   stop()
  return(BranchIdx)
}